package xyz.caijiwang.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import xyz.caijiwang.entity.Article;

import xyz.caijiwang.entity.Comment;
import xyz.caijiwang.entity.Page;
import xyz.caijiwang.entity.User;
import xyz.caijiwang.entity.res.CommentRes;
import xyz.caijiwang.interceptor.TokenNotValidation;
import xyz.caijiwang.mapper.ArticleMapper;

import xyz.caijiwang.mapper.CommentMapper;
import xyz.caijiwang.response.R;
import xyz.caijiwang.service.ArticleService;
import xyz.caijiwang.service.BestService;
import xyz.caijiwang.utils.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ArticleService articleService;

    @Autowired
    BestService bestService;

    @Autowired
    CommentMapper commentMapper;

    /**
     * 获取所有文章集合
     *
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getarticleall")
    public List<Article> getarticleall() {
        return articleMapper.selectAll();
    }

    /**
     * 获取所有文章简介
     *
     * @return
     */
    @TokenNotValidation
    @GetMapping("/selectAllWithoutArticle")
    public List<Article> selectAllWithoutArticle() {
        return articleMapper.selectAllWithoutArticle();
    }


    /**
     * 通过id获取文章
     *
     * @param id
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getArticleById/{id}")
    public Article getArticleById(@PathVariable Integer id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    /**
     * 管理员新增文章
     *
     * @param article
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/insertArticle")
    public int insertArticle(@RequestBody Article article) {
        article.setCreatdate(new Date());
        article.setDelsign(false);
        articleMapper.insert(article);
        return article.getArticleid();
    }

    /**
     * 获取文章分页信息
     *
     * @param page
     * @return
     */
    @PostMapping("/pageGetAllArticle")
    public PageInfo<Article> pageGetAllArticle(@RequestBody Page page) {
        return articleService.pageGetAllArticle(page.getPageNum(), page.getPageSize());
    }

    /**
     * 管理员更新文章
     *
     * @param article
     */
    @PutMapping("/updateArticle")
    public void updateArticle(@RequestBody Article article) {
        articleService.updateArticle(article);
    }

    /**
     * 管理员删除文章（假删除）
     *
     * @param articleid
     */
    @DeleteMapping("/delArticle/{articleid}")
    public void delArticle(@PathVariable Integer articleid) {
        articleService.delArticle(articleid);
    }

    /**
     * 获取文章的点赞
     *
     * @param articleid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getBestCountWithArticleId/{articleid}")
    public int getBestCountWithArticleId(@PathVariable Integer articleid) {
        return bestService.getBestCountWithArticleid(articleid);
    }

    /**
     * 判断本人是否点赞
     *
     * @param request
     * @param articleid
     * @return
     */
    @GetMapping("/checkWeatherIsSelected/{articleid}")
    public Boolean checkWeatherIsSelected(HttpServletRequest request, @PathVariable Integer articleid) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        return bestService.checkWeatherIsSelected(userid, articleid);
    }

    /**
     * 点赞或取消
     *
     * @param request
     * @param articleid
     */
    @PutMapping("likeOrCancel/{articleid}")
    public void likeOrCancel(HttpServletRequest request, @PathVariable Integer articleid) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        Boolean aBoolean = bestService.checkWeatherIsSelected(userid, articleid);
        if (aBoolean) {
            bestService.abolishGiveThumbsUp(articleid, userid);
        } else {
            bestService.giveThumbsUp(articleid, userid);
        }
    }

    /**
     * 通过文章id获得文章的评论
     *
     * @param articleid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getCommentsByArticle/{articleid}")
    public List<CommentRes> getCommentsByArticle(@PathVariable Integer articleid) {
        return articleService.getCommentsByArticle(articleid);
    }

    /**
     * 通过评论的父id获取该父评论的评论者信息
     *
     * @param parentid
     * @return
     */
    @TokenNotValidation
    @GetMapping("/getUserInfoByParentid/{parentid}")
    public User getUserInfoByParentid(@PathVariable Integer parentid) {
        return articleService.getUserInfoByParentid(parentid);
    }

    /**
     * 新增父评论
     *
     * @param comment
     */
    @PostMapping("/addFatherComment")
    public void addFatherComment(HttpServletRequest request, @RequestBody Comment comment) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        comment.setUserid(userid);
        comment.setParentid(0);
        comment.setCreattime(new Date());
        articleService.addFatherComment(comment);

    }

    /**
     * 新增子评论
     *
     * @param request
     * @param comment
     */
    @PostMapping("/addChildComment")
    public void addChildComment(HttpServletRequest request, @RequestBody Comment comment) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        comment.setUserid(userid);
        comment.setCreattime(new Date());
        articleService.addChildComment(comment);
    }

    @DeleteMapping("/delCommentByCommentId/{commentid}")
    public R delCommentByCommentId(HttpServletRequest request, @PathVariable Integer commentid) {
        String token = request.getHeader("Authorization");
        Integer userid = JwtUtil.getUserId(token.substring(7));
        commentMapper.deleteByParentId(commentid, userid);
        commentMapper.deleteByPrimaryKey(commentid, userid);
        return R.success();
    }
}
